package model;

import entities.User;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
import ui.components.ShowEmployeeGridPanel;

public class EmployeeTableModel extends AbstractTableModel {

    private ArrayList<User> list;
    private ArrayList<User> disp;
    private ShowEmployeeGridPanel gridPanel;

    public EmployeeTableModel() {
        list = new ArrayList<User>();
        disp = new ArrayList<User>();
    }

    public EmployeeTableModel(ArrayList<User> list) {
        this.list = new ArrayList<User>(list);
        this.disp = new ArrayList<User>(list);
    }
    
    public void setGridPanel(ShowEmployeeGridPanel gridPanel) {
        this.gridPanel = gridPanel;
    }
    
    public ArrayList<User> getStore() {
        return disp;
    }

    @Override
    public int getRowCount() {
        return disp.size();
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        User user = disp.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return user.getLoginID();
            case 1:
                return user.getName();
            case 2:
                return user.getDepartmentName();
            case 3:
                return user.getRole();
            case 4:
                return user.getLevel();                
            case 5:
                return user.getHireDate();
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return String.class;
            case 2:
                return String.class;
            case 3:
                return Integer.class;
            case 4:
                return Integer.class;
            case 5:
                return Date.class;
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "ID";
            case 1:
                return "Name";
            case 2:
                return "Department";
            case 3:
                return "Role";
            case 4:
                return "Level";                
            case 5:
                return "HireDate";
            default:
                return null;
        }
    }
    
    public void resetShowGrid() {
        gridPanel.includeModel();
    }

    @Override
    public void fireTableDataChanged() {        
        super.fireTableDataChanged();
    }

    public boolean addAll(ArrayList<User> list) {
        boolean returnVal = this.list.addAll(list);
        if (returnVal) {
            fireTableDataChanged();
            resetShowGrid();
        }
        return returnVal;
    }

    public void addElement(User user) {
        list.add(user);
        disp.add(user);
        fireTableDataChanged();
        resetShowGrid();
    }

    public User getAt(int index) {
        return disp.get(index);
    }

    public void removeEmployee(User employee) {
        list.remove(employee);
        disp.remove(employee);
        fireTableDataChanged();
        resetShowGrid();
    }

    public void update(int index, User user) {
        list.set(index, user);
        fireTableDataChanged();
        resetShowGrid();
    }

    public void update(User user) {
        list.set(list.indexOf(user), user);
        int index = disp.indexOf(user);
        if (index != -1) {
            disp.set(index, user);
        }
        fireTableDataChanged();
        resetShowGrid();
    }

    public boolean containtID(String id) {
        for (User u : list) {
            if (u.getLoginID().equals(id)) {
                return true;
            }
        }
        return false;
    }

    public void filter(String id, String name, int depID, int role, int level, Date fromDate, Date toDate) {
        disp.clear();
        for (User u : list) {
            boolean accept = true;
            if (id != null && id.length() != 0) {
                int length = id.length();
                if (u.getLoginID().length() >= length) {
                    if (!u.getLoginID().substring(0, length).equalsIgnoreCase(id)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (name != null && name.length() != 0) {
                int length = name.length();
                if (u.getName().length() >= length) {
                    if (!u.getName().substring(0, length).equalsIgnoreCase(name)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (depID != 0) {
                if (u.getDepartmentID() != depID) {
                    accept = false;
                }
            }
            if (role != 0) {
                if (u.getRole() != role + 1) {
                    accept = false;
                }
            }
            if (level != 0) {
                if (u.getLevel() != level) {
                    accept = false;
                }
            }
            if (fromDate != null) {
                if (u.getHireDate().before(fromDate)) {
                    accept = false;
                }
            }
            if (toDate != null) {
                if (u.getHireDate().after(toDate)) {
                    accept = false;
                }
            }
            if (accept) {
                disp.add(u);
            }
        }
        fireTableDataChanged();
        resetShowGrid();
    }
}
